package com.buba.config;

import com.buba.annotation.MyLog;
import com.buba.config.service.MyUserDetailService;
import com.buba.mapper.LgLogMapper;
import com.buba.pojo.LgLog;
import com.buba.pojo.LgUser;
import com.buba.service.LgLogService;
import com.guli.common.ov.R;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;


@Aspect
@Component
public class OperaLogAspect {

    private Logger logger = LoggerFactory.getLogger(OperaLogAspect.class);

    @Autowired
    private MyUserDetailService userDetailService;
    @Autowired
    private LgLogMapper lgLogMapper;

    @Pointcut("execution(* com.buba.controller..*(..))")
    private void pointcut() {
    }

    @AfterReturning(value = "pointcut()", returning = "obj")
    public void after(JoinPoint joinPoint, Object obj) throws Throwable {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        // 反射加载切点类，遍历类中所有的方法
        Class<?> targetClass = Class.forName(className);
        Method[] methods = targetClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equalsIgnoreCase(methodName)) {
                Class<?>[] clazzs = method.getParameterTypes();
                if (clazzs.length == args.length) {
                    MyLog annotation = method.getAnnotation(MyLog.class);
                    if (annotation != null) {
                        R r = (R) obj;
                        if (r.getSuccess()) {
                            logger.info("日志开始添加=======================================");
                            String desc = annotation.desc();
                            String module = annotation.module();
                            String operatingType = annotation.operatingType();
                            HashMap<String, Object> map = userDetailService.getMap();
                            LgLog lgLog = new LgLog();
                            lgLog.setOperationType(operatingType);
                            LgUser lguser = (LgUser) map.get("lgUser");
                            lgLog.setOperationName(lguser.getUserName());
                            lgLog.setOperationBody(desc);
                            lgLog.setOperationModuel(module);
                            lgLogMapper.insert(lgLog);
                            logger.info("日志已经添加=======================================");
                        }

                    }
                }
            }
        }
    }

}
